package com.ngkj.zmfptback.sys.controller;

import com.alibaba.fastjson.JSON;
import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.sys.bean.UserEntity;
import com.ngkj.zmfptback.sys.service.EmailService;
import com.ngkj.zmfptback.sys.service.ValidateCodeService;
import com.ngkj.zmfptback.sys.service.write.UserWriteService;
import com.ngkj.zmfptback.sys.util.ValidateCodeUtil;
import io.lettuce.core.dynamic.annotation.Param;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author ChenHN
 * @since 2021/12/7 creation
 **/
@Slf4j
@RestController
public class UserLoginAndRegisterController {

    @Resource
    private ValidateCodeService validateCodeService;

    @Resource
    private EmailService emailService;

    @Resource
    private UserWriteService userWriteService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 获取图片验证码
     * @return
     * @throws IOException
     */
    @GetMapping("/getValidateCode")
    public CommonReslut<Map<String,Object>> getValidateCode() throws IOException {
        try {
            Map<String, Object> map = validateCodeService.getVerocde();
            return new CommonReslut(ResultEnum.ok,map);
        } catch (IOException e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error,null);
        }
    }

    /**
     * 获取邮箱验证码
     * @param email
     * @return
     */
    @GetMapping("/getEmailValidate")
    public CommonReslut getEmailValidate(String email){
        if (emailService.isEmail(email)) {
            emailService.sendVercode(email);
            return new CommonReslut("邮箱验证码发送成功，请注意查收！",200);
        }else{
            return new CommonReslut(ResultEnum.error);
        }
    }

    /**
     * 注册
     * @param user
     * @param emailValidate
     * @return
     */
    @PostMapping("/registerUser")
    public CommonReslut registerUser(UserEntity user , String emailValidate){
        String cacheName = "email_"+user.getEmail();
        String trueValidate = (String) redisTemplate.opsForValue().get(cacheName);
        try {
            if (StringUtils.isEmpty(trueValidate)){
                return new CommonReslut("该邮箱验证码过期、验证码不存在！",400);
            }else {
                if (!trueValidate.equals(emailValidate)){
                    return new CommonReslut("该邮箱验证码错误！",400);
                }else{
                    user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
                    user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    boolean b = userWriteService.registerUser(user);
                    if (b){
                        return new CommonReslut("用户注册成功！",200);
                    }else{
                        return new CommonReslut("邮箱已被注册！",400);
                    }
                }
            }
        } catch (Exception e) {
            log.info("注册用户出错，错误原因：{}",e.getMessage());
            return new CommonReslut(ResultEnum.error);
        }
    }

}
